import 'dart:math';

import 'package:dailoz/size_config.dart';
import 'package:dailoz/utils/custom_verification_code_painter.dart';
import 'package:flutter/material.dart';

class CustomVerificationCode extends StatefulWidget {
  final String? code;
  final int? dotCount;
  final double? height;
  final double? width;
  final Color? backgroundColor;

  const CustomVerificationCode(
      {Key? key,
      required this.code,
      this.dotCount = 50,
      this.height = 40,
      this.width = 110,
      this.backgroundColor})
      : super(key: key);

  @override
  State<CustomVerificationCode> createState() => _CustomVerificationCodeState();
}

class _CustomVerificationCodeState extends State<CustomVerificationCode> {
  // 随机生成绘图数据
  Map<String?, dynamic> getRandomData() {
    List<String>? list = widget.code?.split("pattern");
    // x坐标
    double x = 0.0;
    // 最大字体大小
    double maxFontSize = 35.0;
    // 将painter保存起来，先计算出位置
    List mList = [];
    for (String? item in list!) {
      Color color = Color.fromARGB(255, Random().nextInt(255),
          Random().nextInt(255), Random().nextInt(255));
      int fontWeight = Random().nextInt(9);
      TextSpan span = TextSpan(
        text: item,
        style: TextStyle(
          color: color,
          fontWeight: FontWeight.values[fontWeight],
          fontSize: maxFontSize - Random().nextInt(10),
        ),
      );
      TextPainter painter =
          TextPainter(text: span, textDirection: TextDirection.ltr);
      painter.layout();
      double y = Random()
              .nextInt(SizeConfig.getProportionateScreenHeight(widget.height!)
                  .toInt())
              .toDouble() -
          painter.height;
      if (y < 0) {
        y = 0;
      }
      Map<String?, Object> strMap = {"painter": painter, "x": x, "y": y};
      mList.add(strMap);
      x += painter.width + 3;
    }
    double offsetX =
        (SizeConfig.getProportionateScreenWidth(widget.width!) - x) / 2;
    List<dynamic> dotData = [];
    // 绘制干扰点
    for (int i = 0; i < widget.dotCount!; i++) {
      int r = Random().nextInt(255);
      int g = Random().nextInt(255);
      int b = Random().nextInt(255);
      double x = Random()
          .nextInt(
              SizeConfig.getProportionateScreenWidth(widget.width!).toInt() - 5)
          .toDouble();
      double y = Random()
          .nextInt(
              SizeConfig.getProportionateScreenHeight(widget.height!).toInt() -
                  5)
          .toDouble();
      double dotWidth = Random().nextInt(6).toDouble();
      Color color = Color.fromARGB(255, r, g, b);
      Map<String?, dynamic> dot = {
        "x": x,
        "y": y,
        "dotWidth": dotWidth,
        "color": color
      };
      dotData.add(dot);
    }

    Map<String?, dynamic> checkCodeDrawData = {
      "painterData": mList,
      "offsetX": offsetX,
      'dotData': dotData,
    };
    return checkCodeDrawData;
  }

  @override
  Widget build(BuildContext context) {
    // print("build?");
    double maxWidth = 0.0;
    Map<String?, dynamic> drawData = getRandomData();
    // 计算最大宽度做自适应
    maxWidth = getTextSize(
      "8" * widget.code!.length,
      TextStyle(fontWeight: FontWeight.values[8], fontSize: 25),
    ).width;
    return Container(
      color: widget.backgroundColor,
      width: maxWidth > widget.width!.toDouble() ? maxWidth : widget.width,
      height: widget.height,
      child: CustomPaint(
        painter: CustomVerificationCodePainter(drawData: drawData),
      ),
    );
  }

  Size getTextSize(String text, TextStyle textStyle) {
    final TextPainter textPainter = TextPainter(
      text: TextSpan(text: text, style: textStyle),
      maxLines: 1,
      textDirection: TextDirection.ltr,
    )..layout(minWidth: 0, maxWidth: double.infinity);
    return textPainter.size;
  }
}
